package com.taoyibao.hongcanting.ui.mine.delegate;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.taoyibao.hongcanting.R;
import com.taoyibao.hongcanting.base.delegate.BackToolbarDelegate;
import com.taoyibao.hongcanting.constant.CodeConstant;
import com.taoyibao.hongcanting.constant.NetCode;
import com.taoyibao.hongcanting.model.DishModel;
import com.taoyibao.hongcanting.model.OrderInfoModel;
import com.taoyibao.hongcanting.model.SanFangBaseModel;
import com.taoyibao.hongcanting.ui.home.views.SecondaryItemCartArea;
import com.taoyibao.hongcanting.utils.CodeUtils;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.List;

/**
 * @author TangAnna
 * @description: 订单详情
 * @date :${DATA} 14:11
 */
public class OrderDetailDelegate extends BackToolbarDelegate {
    private RecyclerView mRecyclerView;

    @Override
    public int getRootLayoutId() {
        return R.layout.activity_order_detail;
    }

    @Override
    public void initWidget() {
        super.initWidget();
        setTitle("订单详情");
        mRecyclerView = get(R.id.rv_orderDetail_list);
    }

    public void setData(SanFangBaseModel<OrderInfoModel> orderInfoModelSanFangBaseModel) {
        if (orderInfoModelSanFangBaseModel.Code == NetCode.HTTP_200 && orderInfoModelSanFangBaseModel.Data != null) {
            setEmptyView(false);
            OrderInfoModel data = orderInfoModelSanFangBaseModel.Data;
            setListData(data.order_dishs);
            //店名
            if (!TextUtils.isEmpty(data.order_companyname)) {
                ((TextView) get(R.id.tv_orderDetail_name)).setText(data.order_companyname);
            }
            //就餐方式
            if (!TextUtils.isEmpty(data.order_deskid)) {//堂食
                ((TextView) get(R.id.tv_orderDetail_way)).setText(CodeUtils.getIdString(R.string.string_zhuoWei));
                ((TextView) get(R.id.tv_orderDetail_nub)).setText(data.order_deskid);
            } else {//外带
                ((TextView) get(R.id.tv_orderDetail_way)).setText(CodeUtils.getIdString(R.string.string_jiuCanWay));
                ((TextView) get(R.id.tv_orderDetail_nub)).setText(CodeUtils.getIdString(R.string.string_waiDai));
            }
            //订单ID
            if (!TextUtils.isEmpty(data.order_id)) {
                ((TextView) get(R.id.tv_orderDetail_orderId)).setText(data.order_id);
            }
            //备注
            if (!TextUtils.isEmpty(data.order_remark)) {
                String remark = data.order_remark;
                if (remark.contains(CodeConstant.ZENG_SONG)) {//判断包不包括赠送的标记符
                    String[] split = remark.split(CodeConstant.ZENG_SONG);
                    remark = split[0];
                }
                if (!TextUtils.equals(remark, "0")) {
                    ((TextView) get(R.id.tv_orderDetail_remark)).setText(remark);
                }
            }
            //总价
            ((TextView) get(R.id.tv_orderDetail_totalAmout)).setText(CodeUtils.formatAmountUnit(data.order_totalmoney));
            //优惠价格
            ((TextView) get(R.id.tv_orderDetail_couponAmout)).setText(CodeUtils.formatAmountUnit(data.dish_couponmoney));
            //实际支付金额
            ((TextView) get(R.id.tv_orderDetail_payAmount)).setText(CodeUtils.formatAmountUnit(data.order_paymoney));
            //时间
            if (!TextUtils.isEmpty(data.order_addtime)) {
                ((TextView) get(R.id.tv_orderDetail_time)).setText(data.order_addtime);
            }
        } else {
            setEmptyView(true);
        }
    }

    /**
     * 设置列表数据
     *
     * @param data
     */
    public void setListData(List<DishModel> data) {

        if (data == null || data.size() < 1) {
            return;
        }
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
        mRecyclerView.setAdapter(new CommonAdapter<DishModel>(getActivity(), R.layout.item_shopping_cart_secondary_shell, data) {
            @Override
            protected void convert(ViewHolder holder, DishModel dishModel, int position) {
                SecondaryItemCartArea secondaryItemCartArea = (SecondaryItemCartArea) holder.getConvertView();
                secondaryItemCartArea.setData(dishModel);
                secondaryItemCartArea.isShowCountController(false);
            }
        });
    }

    public void setEmptyView(boolean b) {
        get(R.id.layout_orderDetail_empty).setVisibility(b ? View.VISIBLE : View.GONE);
        get(R.id.layout_orderDetail_data).setVisibility(b ? View.GONE : View.VISIBLE);

    }
}
